<?php

namespace app\admin\controller;

use app\admin\model\MemberAttestation;
use app\admin\model\Member;
use think\Db;

class User extends AdminBase
{

    public function userList()
    {
        $users = model('User')->getUserList();
        $this->assign("users", $users);
        return $this->fetch();
    }

    public function getUser()
    {
        $userId = input('param.user_id');
        return model("User")->getUser($userId);
    }

    public function getRoles()
    {

        $roles = model('Role')->getSelectRoles();

        return $roles ? json(['code' => 1, 'roles' => $roles, 'msg' => '获取数据成功']) : json(['code' => 0, 'roles' => '', 'msg' => '获取数据成功']);
    }

    public function addUser()
    {
        $input = input();
        return model('User')->addUser($input);
    }

    public function delUser()
    {
        $userId = input('param.user_id');
        return model('User')->delUser($userId);
    }

    public function updateUser()
    {
        $input = input();
        return model('User')->updateUser($input);
    }

    public function getAuthenticaList()
    {
        $data = MemberAttestation::getInstance()->getList();
        $this->assign("data", $data['data']);
        $this->assign("page", $data['page']);
        return $this->fetch();
    }

    public function checkAuth()
    {
        $id = intval(input('id'));
        $status = intval(input('status'));
        $data = MemberAttestation::getInstance()->getOne(array('id' => $id));
        if (!$data) {
            $this->error('操作失败，请重试');
        }
        Db::startTrans();
        try {
            MemberAttestation::getInstance()->where(array('id' => $id))->update(array('status' => $status));
            if ($status == 1) {
                $update_data = array(
                    'is_authentication' => 2
                );
            } else {
                $update_data = array(
                    'is_authentication' => 3
                );
            }
            Member::getInstance()->where(array('id' => $data['uid']))->update($update_data);
            Db::commit();
            $this->success('操作成功', 'getList');
        } catch (\Exception $e) {
            Db::rollback();
            $this->error('操作失败，请重试');
        }
    }


}
